package com.zhitong.money.services;

import com.zhitong.money.entity.ScheduleTime;
import com.zhitong.money.mapper.ScheduleTimeMapper;
import com.zhitong.money.services.api.IScheduleTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: Andy Zhang
 * @Description:
 * @Date: 16:25 2021/3/17
 * @Company: wisecard
 */
@Service
public class ScheduleTimeService  implements IScheduleTimeService{

    @Autowired
    private ScheduleTimeMapper scheduleTimeMapper;

    @Override
    public ScheduleTime getScheduleTimeByCode(String code){
        return scheduleTimeMapper.getByCode(code);
    }

    @Override
    public void updateTimeByCode(Map<String,Object> map) throws Exception {
        String day = map.get("day").toString();
        String[] split = map.get("time").toString().split(":");
        String cron = new String();
        if (day==null || day.equals("")){

             cron = Integer.parseInt(split[2])+" "+Integer.parseInt(split[1])+" "+Integer.parseInt(split[0])+" "+"?"+" "+"*"+" "+"MON-FRI";
        }else {
             cron = Integer.parseInt(split[2])+" "+Integer.parseInt(split[1])+" "+Integer.parseInt(split[0])+" "+Integer.parseInt(day)+" "+"*"+" "+"?";
        }
        ScheduleTime scheduleTime = new ScheduleTime();
        scheduleTime.setCode(map.get("code").toString());
        scheduleTime.setCron(cron);
        scheduleTimeMapper.updateByCode(scheduleTime);
    }

    @Override
    public List<ScheduleTime> getAll() throws Exception {
        return scheduleTimeMapper.getAll();
    }
}
